// pages/cart/index.js
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        totalnum: 0,
        totalmoney: 0,
        recommend: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getdata()
    },
    checktap: function(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var cart = that.data.cart
        cart[index].checked = !cart[index].checked
        that.setData({
            cart: cart
        })
        that.gettotalnum()
        that.gettotalmoney()
    },
    addnum: function(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        var cart = that.data.cart
        var num = cart[index].num
        num++
        utils.GET({
			url: 'Cart/addcount',
            params: {
                id: id,
				num:num
            },
            success: function(res) {
                console.log(res.data)
				if (res.data.status == 1) {
					cart[index].num=num
					that.setData({
						cart: cart
					})
					that.gettotalnum()
					that.gettotalmoney()
				}
                
            },
        })
        // that.setData({
        //     cart: cart
        // })
     
    },
    cutnum: function(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var cart = that.data.cart
		var id = e.currentTarget.dataset.id
		var num = cart[index].num
		if (num > 0) {
			num--
        }
		utils.GET({
			url: 'Cart/minuscount',
			params: {
				id: id,
				num: num
			},
			success: function (res) {
				console.log(res.data)
				if (res.data.status == 1) {
					cart[index].num = num
					that.setData({
						cart: cart
					})
					that.gettotalnum()
					that.gettotalmoney()
				}

			},
		})
        // that.setData({
        //     cart: cart
        // })
        // that.gettotalnum()
        // that.gettotalmoney()
    },
    deletetap: function(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var cart = that.data.cart
		var id = e.currentTarget.dataset.id
		utils.GET({
			url: 'Cart/delcart',
			params: {
				id: id
			},
			success: function (res) {
				console.log(res.data)
				if (res.data.status == 1) {
					wx.showModal({
						content: '确定删除此商品吗?',
						success(res) {
							if (res.confirm) {
								cart.splice(index, 1)
								that.setData({
									cart: cart
								})
							}
						}
					})
				}

			},
		})

    },
    gettotalnum: function() {
        var that = this
        var cart = that.data.cart
        var total = 0
        for (var i = 0; i < cart.length; i++) {
            if (cart[i].checked == true) {
                total += cart[i].num
            }
        }
        that.setData({
            totalnum: total
        })
    },
    gettotalmoney: function() {
        var that = this
        var cart = that.data.cart
        var total = 0
        for (var i = 0; i < cart.length; i++) {
            if (cart[i].checked == true) {
                total += cart[i].price * cart[i].num
            }
        }
        that.setData({
            totalmoney: total
        })
    },
    getdata: function() {
        var that = this
        utils.GET({
            url: 'Cart/cart',
            params: {
				id: 60765
			},
            success: function(res) {
                console.log(res.data)
                that.setData({
                    recommend: res.data.g,
                    cart: res.data.d
                })
            },
        })
    },
	gobuy:function(){
		wx.navigateTo({
			url: '/pages/product/submit',
		})

	}
})